package main

import (
	"fmt"
	"go-web/src/db-demo/utils"
)

func main() {

	user, err := FindUserById(3)
	if err != nil {
		fmt.Printf("根据ID查询用户失败: %v\n", err)
	} else {
		fmt.Printf("User: %v\n", user)
	}
}

type User struct {
	Id       int
	Username string
	Password string
	Email    string
}

func FindUserById(id int) (user *User, err error) {
	sqlStr := "select username, password, email from go_user where id=?"
	stmt, err := utils.Db.Prepare(sqlStr)
	if err != nil {
		return nil, err
	}

	row := stmt.QueryRow(id)

	var username string
	var password string
	var email string
	err = row.Scan(&username, &password, &email)
	if err != nil {
		return nil, err
	}

	return &User{
		Id:       id,
		Username: username,
		Password: password,
		Email:    email,
	}, nil
}
